// pages/donation/donation.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    money:[1,10,50,100,1000],
    message:'',
    active: '10',
    messageLength: 0,
    messageMaxLength: 200,
    encryptedData: null,
    iv: null,
    userId:'',
    openId:'',
    payMoney:''       //捐款金额
  },
  // 金额切换
  switch(e) {
    let index = e.currentTarget.dataset.switchType || ''
    console.log(index)
    this.setData({
      active: index,
      payMoney: index
    })
    if (index == '其他') {
      this.setData({
        payMoney: ''
      })
    }
  },
  // 留言板
  bindTextAreaBlur(e) {
    if (e.detail && e.detail.value.length > 0){
      this.setData({
        message: e.detail.value
      })
    }
  },
  bindTextAreaInput(e) {
    if (e.detail && e.detail.cursor >= 0) {
      this.setData({
        messageLength: e.detail.cursor
      })
    }
  },
  // 提交
  submit() {    
    if (this.checkMoney(this.data.payMoney)){
      let openId = wx.getStorageSync("openId")
      if (openId != null && openId != '') {
          this.setData({
            openId: openId,
            userId: wx.getStorageSync("userId")
          })
          this.orderSave()
      } else {
        wx.getUserInfo({
          success: res => {
            wx.setStorageSync('userInfo', res.userInfo)
            this.setData({
              encryptedData: res.encryptedData,
              iv: res.iv
            })
            this.getUserId()
          }
        })
      }     
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      active: 10,
      payMoney: 10
    })
  },
  /*
   *其他金额输入
   */
  setOtherMoney: function (e) {
    this.setData({
      payMoney: e.detail.value
    })
  },
  /**
     * 获取用户ID
     */
  getUserId() {
    let _this = this
    wx.login({
      success: function (res) {
        if (res.code) {
          wx.request({
            url: getApp().globalData.api + '/a/wxinterface/setUserInfo', //
            data: {
              js_code: res.code,
              encryptedData: _this.data.encryptedData,
              iv: _this.data.iv,
              payMoney: _this.data.payMoney
            },
            header: {
              'content-type': 'application/json' // 默认值
            },
            success: res => {
              wx.setStorageSync('userId', res.data.obj.userId)
              wx.setStorageSync('openId', res.data.obj.openId)
              _this.setData({
                userId: res.data.obj.userId,
                openId: res.data.obj.openId
              })
              _this.orderSave()
            }
          })
        }
      }
    })
  },
  /**
   * 订单保存
   */
  orderSave() {
    let _this = this
    // console.log(_this)
    wx.request({
      url: getApp().globalData.api + '/a/wxinterface/open/orderSave', //
      data: {
        eventId: getApp().globalData.eventId,
        userId: _this.data.userId,
        leaveMessage: _this.data.message,
        money: _this.data.payMoney,
        isNiming: 2,
        source: 'ql'
      },
      success: function (res) {
        // console.log(res)
        wx.request({
          url: 'https://www.qqchou.org/zfzx/api', //
          data: {
            appKey: '8afac34063d2e8620163d2f22eb50000',
            type: '11',
            orderInfo: {
              count: 1,
              product_id: res.data.obj.orderId,
              product_name: '青联公益',
              price: _this.data.payMoney
            },
            callBackUrl: res.data.obj.callBackUrl,
            openid: _this.data.openId
          },
          success: function (res) {
            //console.log(res)
            wx.requestPayment(
              {
                'timeStamp': res.data.obj.timeStamp,
                'nonceStr': res.data.obj.nonceStr,
                'package': res.data.obj.packageStr,
                'signType': 'MD5',
                'paySign': res.data.obj.paySign,
                'success': function (res) {
                  wx.navigateTo({
                    url: '../../pages/personal/record/record'
                  })                 
                },
                'fail': function (res) {
                }
              })
          }
        })
      }
    })
  },
  checkMoney(payMoney) {
    var boo;
    var reg = /^((\d*.\d{0,2})|(0.\d{0,2}))$/;
    var _content = ''
    if (payMoney == '') {
      boo = false;
      _content = '请选择金额或输入金额';
    } else {
      boo = reg.test(payMoney)
      _content = '请输入正确的金额数！（提示：只能是数字或精确到两位小数）';
    }
    if (!boo) {
      wx.showModal({
        title: '提示',
        content: _content,
        showCancel: false
      })
    }
    return boo
  }
})





